package com.txhy.certificatechain.presenter.user;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.txhy.certificatechain.R;
import com.txhy.certificatechain.api.UrlAddress;
import com.txhy.certificatechain.presenter.base.BasePresenter;
import com.txhy.certificatechain.ui.view.user.UpLoadView;
import com.txhy.certificatechain.utils.GsonUtil;
import com.txhy.certificatechain.utils.UIUtils;

import java.io.File;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2030, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: UpLoadPresenter.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 上传身份证信息
 * 创建时间: 2018/9/20 17:41
 * 更新时间：2018/9/20 17:41
 * -----------------------------------------------------------------
 * 2018/9/20 17:41: 创建 UpLoadPresenter.java (侯建军);
 * -----------------------------------------------------------------
 */
public class UpLoadPresenter extends BasePresenter<UpLoadView> {
    public void upLoadImg(String filePath, final String msg) {
        getView().showProgress(UIUtils.getString(R.string.zhengzaishang));

        OkGo.<String>post(UrlAddress.BASE_IMGURL)//
                .tag(this)//
                .params("file", new File(filePath))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().upLoadSuccess(msg, GsonUtil.GsonGetData(response.body()));
                        } else {
                            getView().upLoadFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                        getView().hideProgress();
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().upLoadFail("上传失败，请重新上传");
                        getView().hideProgress();
                    }
                });
    }

    /**
     * 身份证上传信息
     * @param userJson
     */
    public void SendCard(String userJson) {
        getView().showProgress("正在上传数据");
        OkGo.<String>post(UrlAddress.SAVEIDCARD_URL)//
                .tag(this)//
                .params("userJson", userJson)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().upMsgSuccess(GsonUtil.GsonGetMsg(response.body()));
                        } else {
                            getView().upMsgFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                        getView().hideProgress();
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().upMsgFail("请求失败请检查网络");
                        getView().hideProgress();
                    }
                });
    }
}

